export default BeautifulPolygon

import {c, mouse} from "./canvas";
import "./utils"
import {randomIntFromRange} from "./utils";

//较为均匀的多边形（指以中心为坐标系的每个象限都存在点）
// n为边数
// x y为中心坐标
// a为中心到顶点的距离
// ang为逆时针旋转角度， 默认为0
// warning: do not set n to a large number
function BeautifulPolygon(n, x, y, a, ang = 0, color = 'white') {
    this.n = n;
    this.x = x;
    this.y = y;
    this.a = a;
    this.ang = ang - Math.PI * 0.5;
    if (this.ang < 0) {
        this.ang = this.ang + Math.PI * 2;
    }
    this.color = color;
    this.angles = [];

    this.setAngles = function () {

        //tempAng: current angle
        this.angles[0] = this.ang;

        let tempQuadrant = 0;

        for (let i = 1; i < this.n; ++i) {
            let getRandomAngle = randomIntFromRange(0, 90);
            this.angles[i] = tempQuadrant * 0.5 * Math.PI + getRandomAngle / 360 * 2 * Math.PI;
            while (this.angles[i] >= 2 * Math.PI)
                this.angles[i] = this.angles[i] - 2 * Math.PI;
            tempQuadrant++;
            if (tempQuadrant > 3) tempQuadrant = 0;
        }
        this.angles.sort();
    }
    this.setAngles();
    console.log(this.angles);


    // this.show = function () {
    //     c.beginPath();
    //     let tempAng = this.ang;
    //     const rotateAng = 2 * Math.PI / (n + 1);
    //     c.moveTo(x + this.a * Math.cos(tempAng), y + this.a * Math.sin(tempAng));
    //     for (let i = 0; i < n - 1; ++i) {
    //         let getSign;
    //         if (Math.random() <= 0.5) {
    //             getSign = 1;
    //         } else {
    //             getSign = -1;
    //         }
    //         let getRandomAngle = Math.ceil(Math.random() * 100000) % n * rotateAng;
    //         tempAng = tempAng - rotateAng + getSign * getRandomAngle;
    //         c.lineTo(x + this.a * Math.cos(tempAng), y + this.a * Math.sin(tempAng));
    //     }
    //     c.lineTo(x + this.a * Math.cos(tempAng), y + this.a * Math.sin(tempAng));
    //     c.fillStyle = this.color;
    //     c.fill();
    //     c.stroke();
    //     c.closePath();
    //
    // }

    this.show = function () {
        c.beginPath();
        let tempAng = this.angles[0];
        c.moveTo(this.x + this.a * Math.cos(tempAng), this.y + this.a * Math.sin(tempAng));

        for (let i = 1; i < this.n; ++i) {
            tempAng = this.angles[i];
            c.lineTo(this.x + this.a * Math.cos(tempAng), this.y + this.a * Math.sin(tempAng));
        }
        tempAng = this.angles[0];
        c.lineTo(this.x + this.a * Math.cos(tempAng), this.y + this.a * Math.sin(tempAng));
        c.closePath();
        c.fillStyle = this.color;
        c.fill();
        c.stroke();
    }

    // 一个静态的多边形
    this.update = function () {
        this.show();
    }

    // 一个跟随鼠标转动的多边形
    this.updateWithMouse = function () {
        if (mouse.x === this.x) this.ang = -Math.PI * 0.5;
        else this.ang = Math.atan((mouse.y - this.y) / (mouse.x - this.x));

        this.show();
    }

    // 一个随机转动的多边形
    this.updateWithRandomAngle = function () {
        this.ang = this.ang + Math.random() % 2 * Math.PI;

        this.show();
    }

    this.changeAngle = function (ang) {
        this.ang = ang - Math.PI * 0.5;

    }

    this.changePosition = function (x, y) {
        this.x = x;
        this.y = y;
    }
}